package com.example.SearchAround_JC.jc_activity;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.test.suitebuilder.TestSuiteBuilder;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.*;
import com.example.SearchAround_JC.R;
import com.example.SearchAround_JC.jc_manager.Distance;
import com.example.SearchAround_JC.jc_manager.PoiDetails;
import com.example.SearchAround_JC.jc_manager.PoiResult;
import org.json.JSONException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by jing on 2014/11/30.
 */
public class SearchActivity extends Activity {
    private Button backBt;
    private EditText searchEditText;
    private Button searchBt;
    private ListView searchListView;

    List<PoiDetails> list = new ArrayList<PoiDetails>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.search_activity);


        backBt = (Button) findViewById(R.id.search_back_bt);
        backBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        searchEditText = (EditText) findViewById(R.id.search_EditText);
        searchListView = (ListView) findViewById(R.id.search_listView);

        searchBt = (Button) findViewById(R.id.search_search_bt);
        searchBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getSearchInfo();
            }
        });

        searchListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                PoiDetails poiDetails = (PoiDetails) parent.getItemAtPosition(position);

                Intent intent1 = new Intent(SearchActivity.this, POIdetails_Activity.class);
                intent1.putExtra("name", poiDetails.getName());
                Distance dis = new Distance();
//                distance = dis.deDistance(poiDetails.getDistance());
                intent1.putExtra("distance", dis.deDistance(poiDetails.getDistance()));
                intent1.putExtra("tel",poiDetails.getTel());
                intent1.putExtra("address",poiDetails.getAddress());
                intent1.putExtra("y",poiDetails.getY());
                intent1.putExtra("x",poiDetails.getX());
                startActivity(intent1);
            }
        });

    }

    private void getSearchInfo() {
        final ProgressDialog progressDialog = new ProgressDialog(this);
        AsyncTask<String, Integer, Integer> asyncTask = new AsyncTask<String, Integer, Integer>() {
            @Override
            protected Integer doInBackground(String... params) {
                //获取数据
                PoiResult poiResult = new PoiResult();
                //拿出当前地点的经纬度
                SharedPreferences Lshare = getSharedPreferences("coordinate", MODE_PRIVATE);
                String lat = Lshare.getString("coordinate", "");

                try {
                    list = poiResult.ResultPoi(lat, searchEditText.getText().toString(), "3000", 8);
                    return 0;
                } catch (IOException e) {
                    return 1;
                } catch (JSONException e) {
                    return 2;
                }
            }

            @Override
            protected void onPreExecute() {
                progressDialog.setMessage("正在加载");
                progressDialog.show();
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Integer integer) {
                progressDialog.dismiss();
                if (integer == 0) {
                    searchListViewItemAdapter adapter = new searchListViewItemAdapter(SearchActivity.this,
                            R.layout.poilist_item, list);
                    searchListView.setAdapter(adapter);
                }
                if (integer == 1) {
                    Toast.makeText(SearchActivity.this, "暂无数据", Toast.LENGTH_SHORT).show();
                }
                if (integer == 2) {
                    Toast.makeText(SearchActivity.this, "网络不给力", Toast.LENGTH_SHORT).show();

                }
                super.onPostExecute(integer);
            }

            @Override
            protected void onProgressUpdate(Integer... values) {
                super.onProgressUpdate(values);
            }

            @TargetApi(Build.VERSION_CODES.HONEYCOMB)
            @Override
            protected void onCancelled(Integer integer) {
                super.onCancelled(integer);
            }

            @Override
            protected void onCancelled() {
                super.onCancelled();
            }
        };
        asyncTask.execute();
    }

    class searchListViewItemAdapter extends ArrayAdapter<PoiDetails> {
        int resourceId;

        public searchListViewItemAdapter(Context context, int resource, List<PoiDetails> objects) {
            super(context, resource, objects);
            resourceId = resource;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            PoiDetails p = getItem(position);

            View view = LayoutInflater.from(SearchActivity.this).inflate(resourceId, null);
            view.setLayoutParams(new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,80));
            TextView textView = (TextView) view.findViewById(R.id.poiname_TextView);

            TextView textView1 = (TextView) view.findViewById(R.id.poiaddress_TextView);
            TextView textView2 = (TextView) view.findViewById(R.id.poidistance_TextView);
            textView.setText(p.getName());
            textView1.setText(p.getAddress());

            Distance distance = new Distance();
            String dis = distance.deDistance(p.getDistance());

            textView2.setText(dis);
            return view;
        }
    }


}
